﻿using System;
using MaLu.Common;
using System.Data;
using MaLu.BusiLogic;
using Wicresoft.Framework.Organization;
using MaLu.Data;
using System.Web.UI.WebControls;

namespace MaluJg.web.PovertyIn
{
    public partial class PList : PPage
    {

        public string SearchClauseItem
        {
            get
            {
                if (ViewState["SearchClauseItem"] == null)
                    ViewState["SearchClauseItem"] = "1=1";

                return ViewState["SearchClauseItem"].ToString();
            }
            set
            {
                ViewState["SearchClauseItem"] = value;

            }
        }
        public DataTable DTVillage
        {
            get
            {
                if (ViewState["DTVillage"] == null)
                    ViewState["DTVillage"] = BLPoverty.GetFType(Provider);

                return (DataTable)ViewState["DTVillage"];
            }
            set
            {
                ViewState["DTVillage"] = value;

            }
        }
        protected string Type
        {
            get
            {

                return ddlType.SelectedValue;
            }
        
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "扶贫帮困资金列表";


            tbYear.Attributes.Add("readonly", "readonly");

            if (!IsPostBack)
            {
                this.Order.OrderColumn = "Year";
                this.Order.OrderDirection = "DESC";
                //Department mainD = OrganizationHelper.GetDepartmentInfo(CurrentUser.MainDepartmentID);
                //string[] ids = mainD.IDPath.Split('/');
                //if (ids.Length >= 4)
                //    DepartmentID = new Guid(ids[3]);
                //else
                //    DepartmentID = new Guid(ids[ids.Length - 1]);

                ddlType.DataSource = BLPoverty.GetPType();
                ddlType.DataValueField = "value";
                ddlType.DataTextField = "text";
                ddlType.DataBind();
                ddlVillage.DataSource = DTVillage;
                ddlVillage.DataTextField = "name";
                ddlVillage.DataValueField = "id";
                ddlVillage.DataBind();

                ddlVillage.Items.Insert(0, new ListItem("全部","-1"));

            }

        }

        protected override void Pager_PageChanged(object sender, EventArgs e)
        {
            BindData();
            tbIDs.Text = string.Empty;
            base.Pager_PageChanged(sender, e);
        }

        protected override void Order_OrderChanged(object sender, EventArgs e)
        {
            BindData();
            base.Order_OrderChanged(sender, e);
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            Pager.PageIndex = 0; 
           BindData();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {

            Pager.PageIndex = 0;

            if (tbIDs.Text.Trim() != string.Empty)
            {
                string table = ddlType.SelectedValue;

              
                string sql = string.Format("delete from [{0}] where id in ('{1}');", table, tbIDs.Text.Trim().Substring(1).Replace(",", "','"));

                SqlHelper.Instance.ExecuteNonQuery(Provider, sql);
                tbIDs.Text = string.Empty;
            }

            BindData();
        }
        protected void ddlType_Selected(object sender, EventArgs e)

        {
            Pager.PageIndex = 0;

            SearchClauseItem = "1=1";
            BindData();

        
        }
        private void BindData()
        {
            int recordCount = 0;
            string selectedFields = "ID,[Name],[BelongTo], Money,Year";
            string table ="SDeformity"; 

            string order = "Year desc,Month desc ";
            if (ddlType.SelectedValue != string.Empty)
                table = ddlType.SelectedValue;
            if (ddlType.SelectedValue == "sliving")
            {
                selectedFields = "ID,Name,BelongTo, Money,Year";
                order = "Year desc,ID desc ";
            }

            if (ddlType.SelectedValue == "sstudy")
            {
                selectedFields = "ID,Name,BelongTo, Money,Year";
                order = "Year desc,ID desc ";
            }

            if (ddlType.SelectedValue == "ssick")
            {
                selectedFields = "ID,Name,BelongTo, Money,Year";
                order = "Year desc,ID desc ";
            }
            if (ddlType.SelectedValue == "sothers")
            {
                selectedFields = "ID,Name,BelongTo, Money,Year";
                order = "Year desc,ID desc ";
            }


            DataTable DTMyProposal = BLCommon.GetPagingList(Provider, selectedFields, table, SearchClauseItem, order, Pager.PageIndex + 1, Pager.PageSize, false, 0, ref recordCount);
           
           
            
            Pager.RecordCount = recordCount;
            rptBusi.DataSource = DTMyProposal;
            rptBusi.DataBind();
        }

        protected void btnAdvancedSearch_Click(object sender, EventArgs e)
        {
            string where = "1=1";
            if (tbName.Text.Trim() != string.Empty)
            {
                where += " and Name like '%" + tbName.Text.Trim().Replace("'", "''") + "%'";

            }
            if (tbYear.Text.Trim() != string.Empty)
            {
                where += " and Year='" + tbYear.Text.Trim() + "'";

            }
            if (ddlVillage.SelectedValue != "-1")
            {
                where += " and BelongTo = '" + ddlVillage.SelectedItem.Text + "'";

            }
            SearchClauseItem = where;
            Pager.PageIndex = 0;
            
         BindData();
        }
    }
}
